﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
 * 1.需配置项目packagename
 * 2.项目内购ID生效
 * 3.能够链接谷歌网站
 * 4.订阅购买暂未验证,需自测.
 * **/
public class GoogleIAP : MonoBehaviour
{
    public static GoogleIAP Instance = null;
    AndroidJavaObject JavaPurchase = null;
    string strPayingSKU = string.Empty;

    [RuntimeInitializeOnLoadMethod]
    static void Init()
    {
        GameObject obj = new GameObject("GoogleIAP");
        Instance = obj.AddComponent<GoogleIAP>();
        DontDestroyOnLoad(obj);
    }
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 初始化内购
    /// </summary>
    /// <param name="strArrPurchaseID">内购ID数组</param>
    /// <param name="bIsDebug">true: 调试模式, false: 发行模式</param>
    public void InitGoogleIAP(string[] strArrPurchaseID, bool bIsDebug)
    {
        if (bIsDebug)
        {
            string strThem = string.Empty;
            foreach (var it in strArrPurchaseID)
                strThem += it + "\r\n";
            Debug.Log("RedRainGameSDKMgr::InitPurchase(): " + strThem + bIsDebug);
        }

        if (Application.platform == RuntimePlatform.Android)
        {
            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            var javaUnity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaClass purchase = new AndroidJavaClass("com.dzsoftware.dzsoftwaresdk.GoogleIAP");
            purchase.CallStatic("TestFun");
            JavaPurchase = purchase.GetStatic<AndroidJavaObject>("Instance");

            object[] obj = new object[3];
            obj[0] = javaUnity;
            obj[1] = strArrPurchaseID;
            obj[2] = bIsDebug;


            JavaPurchase?.Call("InitIAP", obj);
        }
    }
    
    public void Pay(string strSKU)
    {
        JavaPurchase?.Call("Pay", strSKU);
        strPayingSKU = strSKU;
    }

    /// <summary>
    /// 支付成功
    /// </summary>
    /// <param name="msg"></param>
    public void JavaCallPaySuccess(string msg)
    {
        Debug.Log(string.Format("GoogleIAP::JavaCallPaySuccess(): msg:{0}", msg));
    }

    /// <summary>
    /// 支付失败
    /// </summary>
    /// <param name="msg"></param>
    public void JavaCallPayFailed(string msg)
    {
        Debug.Log(string.Format("GoogleIAP::JavaCallPayFailed(): msg:{0}", msg));
    }

    /// <summary>
    /// 支付取消
    /// </summary>
    /// <param name="msg"></param>
    public void JavaCallPayCanceled(string msg)
    {
        Debug.Log(string.Format("GoogleIAP::JavaCallPayCanceled(): msg:{0}", msg));
    }

    /// <summary>
    /// 在这个接口做发货处理
    /// </summary>
    /// <param name="strSKU"></param>
    public void JavaCallConsumePurchase(string strSKU)
    {
        Debug.Log(string.Format("GoogleIAP::JavaCallConsumePurchase(): {0}", strSKU));
    }

    /// <summary>
    /// 查询订单,启动的合适时机和每次onResume调用
    /// </summary>
    public void QueryPurchases()
    {
        Debug.Log("GoogleIAP::JavaCallQueryPurchases()");
        JavaPurchase?.Call("QueryPurchases");
    }

    private void OnApplicationFocus(bool focus)
    {
        if (focus)
        {
            QueryPurchases();
        }
    }
}
